﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraCharts;
using Datalayer;
using Entities;
using Common;

namespace QLHoNgheo
{
    public partial class frm_Chart : Form
    {
        public frm_Chart()
        {
            InitializeComponent();
        }
        Series[] series;
        public DataTable dt_KQ1, dt_KQ2;
        IQueryMgr iq;
        private void frm_Chart_Load(object sender, EventArgs e)
        {
            cl_Lookupedit.Lucb_Quanhuyen(ref lk_quan);
        }

        private void lk_quan_EditValueChanged(object sender, EventArgs e)
        {
            int id = int.Parse(lk_quan.EditValue.ToString());
            iq = QueryMgr.GetDbInstance();
            List<HoNgheo> ls = iq.HN_GetHoNgheoByQuanID(id);

            dt_KQ1 = cl_EditAll1Form1.Create_dt(ls);
            cl_Lookupedit.lucb_PhuongXa(ref lk_phuong, id);

           // VeBieuDo(dt_KQ1);
            Count(dt_KQ1);
        }

        private void lk_phuong_EditValueChanged(object sender, EventArgs e)
        {
            iq = QueryMgr.GetDbInstance();
            int id = int.Parse(lk_phuong.EditValue.ToString());
            List<HoNgheo> ls = iq.HN_GetHoNgheoByPhuongID(id);

            dt_KQ1 = cl_EditAll1Form1.Create_dt(ls);
            Count(dt_KQ1);
        }
        void Count(DataTable dt)
        {
            int count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, count6 = 0, count7 = 0, count8 = 0, count9 = 0, count10 = 0, count11 = 0 ;
            int count12 = 0, count14=0;

            for (int i = 0; i < dt.Rows.Count; i++)
            {   
                if (Convert.ToInt32(dt.Rows[i]["ThuNhap"]) <200)
                    count1++;

                else if (200<=Convert.ToInt32(dt.Rows[i]["ThuNhap"])&&Convert.ToInt32(dt.Rows[i]["ThuNhap"])<300)
                    count2++;
                else if (300 <= Convert.ToInt32(dt.Rows[i]["ThuNhap"]) && Convert.ToInt32(dt.Rows[i]["ThuNhap"]) < 400)
                    count3++;
                else if (400 <= Convert.ToInt32(dt.Rows[i]["ThuNhap"]) && Convert.ToInt32(dt.Rows[i]["ThuNhap"]) < 500)
                    count4++;
                else if (500 <= Convert.ToInt32(dt.Rows[i]["ThuNhap"]) && Convert.ToInt32(dt.Rows[i]["ThuNhap"]) < 600)
                    count5++;
                else if (600 <= Convert.ToInt32(dt.Rows[i]["ThuNhap"]) && Convert.ToInt32(dt.Rows[i]["ThuNhap"]) < 700)
                    count6++;
                else if (700 <= Convert.ToInt32(dt.Rows[i]["ThuNhap"]) && Convert.ToInt32(dt.Rows[i]["ThuNhap"]) < 800)
                    count7++;
                else if (800 <= Convert.ToInt32(dt.Rows[i]["ThuNhap"]) && Convert.ToInt32(dt.Rows[i]["ThuNhap"]) < 900)
                    count8++;
                else if (900 <= Convert.ToInt32(dt.Rows[i]["ThuNhap"]) && Convert.ToInt32(dt.Rows[i]["ThuNhap"]) < 1000)
                    count9++;
                else if (1000 <= Convert.ToInt32(dt.Rows[i]["ThuNhap"]) && Convert.ToInt32(dt.Rows[i]["ThuNhap"]) < 1100)
                    count10++;
                else if (Convert.ToInt32(dt.Rows[i]["ThuNhap"]) > 1100)
                    count11++;
            }
            count12 = count1 + count2 + count3 + count4 + count5 + count6;
            count14 = count7 + count8 + count9 + count10 + count11;

            if (dt.Rows.Count != 0)
            {   
                chartControl_GV.Series.Clear();
                Series series1 = new Series("series1", ViewType.Pie3D);
                series1.Points.Add(new SeriesPoint("<200", count1));
                series1.Points.Add(new SeriesPoint("200-300", count2));
                series1.Points.Add(new SeriesPoint("300-400", count3));
                series1.Points.Add(new SeriesPoint("400-500", count4));
                series1.Points.Add(new SeriesPoint("500-600", count5));
                series1.Points.Add(new SeriesPoint("600-700", count6));
                series1.Points.Add(new SeriesPoint("700-800", count7));
                series1.Points.Add(new SeriesPoint("800-900", count8));
                series1.Points.Add(new SeriesPoint("900-1000", count9));
                series1.Points.Add(new SeriesPoint("1000-1100", count10));
                series1.Points.Add(new SeriesPoint(">1100", count11));
                chartControl_GV.Series.Add(series1);
                
                //3d
              
               Pie3DSeriesView myView = (Pie3DSeriesView)series1.View;
                
                // Show a title for the series.
                myView.Titles.Add(new SeriesTitle());
                myView.Titles[0].Text = "Biểu đồ phân loại mức thu nhập theo hộ";
                
                series1.PointOptions.ValueNumericOptions.Format = NumericFormat.Percent;
                series1.PointOptions.ValueNumericOptions.Precision = 1;
                series1.PointOptions.PointView = DevExpress.XtraCharts.PointView.ArgumentAndValues;
                // Adjust the view-type-specific options of the series.
                ((Pie3DSeriesView)series1.View).Depth = 30;
                ((Pie3DSeriesView)series1.View).ExplodedPoints.Add(series1.Points[0]);
                ((Pie3DSeriesView)series1.View).ExplodedDistancePercentage = 20;
               
                // Access the diagram's options.
                ((SimpleDiagram3D)chartControl_GV.Diagram).RotationType = RotationType.UseAngles;
                ((SimpleDiagram3D)chartControl_GV.Diagram).RotationAngleX = -35;
                Pie3DSeriesLabel label = (Pie3DSeriesLabel)series1.Label;
                
              
                label.ResolveOverlappingMode = ResolveOverlappingMode.Default;

                Series series2 = new Series("series2", ViewType.Pie3D);
                series2.Points.Add(new SeriesPoint("Thấp", count12));
                series2.Points.Add(new SeriesPoint("Cao", count14));
                series2.PointOptions.ValueNumericOptions.Format = NumericFormat.Percent;
                series2.PointOptions.ValueNumericOptions.Precision = 1;
                series2.PointOptions.PointView = DevExpress.XtraCharts.PointView.ArgumentAndValues;
                chartControl_GV.Series.Add(series2);
                ((SimpleDiagram3D)chartControl_GV.Diagram).RotationType = RotationType.UseAngles;
                ((SimpleDiagram3D)chartControl_GV.Diagram).RotationAngleX = -35;
                Pie3DSeriesLabel label1 = (Pie3DSeriesLabel)series2.Label;
                ((Pie3DSeriesView)series2.View).Depth = 30;
                ((Pie3DSeriesView)series2.View).ExplodedPoints.Add(series2.Points[0]);
                ((Pie3DSeriesView)series2.View).ExplodedDistancePercentage = 20;
                Pie3DSeriesView myView1 = (Pie3DSeriesView)series2.View;

                // Show a title for the series.
                myView1.Titles.Add(new SeriesTitle());
                myView1.Titles[0].Text = "Biểu đồ phân loại mức thu nhập(cao, thấp)";
                //myView.Titles[0].TextColor = 
                // Add a title to the chart and hide the legend.
                // ChartTitle chartTitle1 = new ChartTitle();
              //   chartTitle1.Text = "3D Pie Chart";
               // chartControl_GV.Titles.Add(chartTitle1);
              //  chartControl_GV.Legend.Visible = true;
              //  chartControl_GV.Legend.TextVisible = true;
               // chartControl_GV.SeriesDataMember.
                // Add the chart to the form.
            }
            else { chartControl_GV.Series.Clear(); }
        }

        void VeBieuDo(DataTable dt_KQ)
        {
            chartControl_GV.Series.Clear();
           
            if (dt_KQ != null)
            {
                series = new Series[dt_KQ.Rows.Count];
                for (int k = 0; k < dt_KQ.Rows.Count; k++)
                {
                    series[k] = new Series();
                    series[k].Name = dt_KQ.Rows[k]["Ma_HoNgheo"].ToString();
                    series[k].Points.Add(new SeriesPoint(dt_KQ.Rows[k]["Ma_HoNgheo"].ToString(), new Double[] { Convert.ToDouble(dt_KQ.Rows[k]["ThuNhap"] + "" == "" ? 0 : dt_KQ.Rows[k]["ThuNhap"]) }));

                    chartControl_GV.Series.Add(series[k]);
                }

            }
        }

        private void chartControl_GV_Click(object sender, EventArgs e)
        {

        }
    }
}
